package com.dhecp.project.storage.service.impl;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.RandomValueUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.project.business.mapper.ProductActiveMapper;
import com.dhecp.project.common.service.UpdateLogService;
import com.dhecp.project.common.service.impl.BaseModuleServiceImpl;
import com.dhecp.project.storage.domain.StockRecord;
import com.dhecp.project.storage.mapper.StockRecordMapper;
import com.dhecp.project.storage.service.StockRecordService;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: kjecp
 * @description: 库存情况记录表
 * @author: hlf
 * @create: 2022-12-21 09:51
 */
@Service
public class StockRecordServiceImpl  extends BaseModuleServiceImpl implements StockRecordService {


    @Autowired
    private StockRecordMapper storageMapper;

    @Autowired
    private UpdateLogService updateLogService;

    @Autowired
    private ProductActiveMapper productActiveMapper;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;


    /**
     * @Description: 查询指定单nid的首记录
     * @Param: [params]
     * @return: com.dhecp.project.storage.domain.StockRecord
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public StockRecord findByGuid(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        return storageMapper.findById(params);
    }


    /**
     * @Description: 查询
     * @Param: [params]
     * @return: java.util.List<com.dhecp.project.storage.domain.StockRecord>
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @Override
    public List<StockRecord> queryList(Map<String, Object> params) throws Exception {
        params.put(DataFiltering.TABLE_ALIAS, "t1");    //查询表别名
        params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));    //常规查询条件SQL语句
        params.put(DataFiltering.DATA_SCODE, dictionaryForFieldService.getDataScode(params));    //数据权限查询条件SQL语句
        PageUtils.startPage();
        return storageMapper.select(params);
    }

    /**
     * @Description: 保存
     * @Param: [params] moduleTable 必要参数：功能唯一标识
     * @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params] infoItems 必要参数：对应实体类的json格式字符串
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:10
     */
    @SuppressWarnings("unchecked")
    @Override
    public AjaxResult saveData(Map<String, Object> params) {

        StockRecord oldData = new StockRecord();

        StockRecord stockRecord = JSON.parseObject(params.get("infoItems").toString(), StockRecord.class);

        Map<String, Object> params2 = new HashMap<String, Object>();
        params2.put("guid",stockRecord.getGuid());
        oldData = storageMapper.findById(params2);


        if (stockRecord.getGuid() != null && !stockRecord.getGuid().equals("")){
            storageMapper.update(stockRecord);
            updateLogService.addUpdateLog(params.get("moduleTable").toString(),stockRecord.getGuid(),oldData,stockRecord,"修改");
        }else{
            stockRecord.setGuid(RandomValueUtils.getGUID());
            stockRecord.setCreateTime(new Date());
            storageMapper.insert(stockRecord);
            updateLogService.addInsertLog(params.get("moduleTable").toString(),stockRecord.getGuid(),"新增");
        }


        return AjaxResult.success(MessageUtils.message("module.success.issuccess"), stockRecord.getGuid());
    }

    /**
     * @Description: 删除
     * @Param: [params]
     * @return: com.dhecp.common.core.domain.AjaxResult
     * @Author: hlf
     * @date: 2022-05-09 10:11
     */
    @Override
    public AjaxResult deleteData(Map<String, Object> params) {
        List<StockRecord> businessDataList = JSON.parseArray(params.get("infoItems").toString(), StockRecord.class);
        if (businessDataList == null || businessDataList.size() == 0) {
            return AjaxResult.error(MessageUtils.message("module.error.jsontoentyerror"));
        }
        for (StockRecord businessData : businessDataList) {
            storageMapper.delete(businessData);
            updateLogService.addInsertLog(params.get("moduleTable").toString(),businessData.getGuid(),"删除");
        }
        return AjaxResult.success(MessageUtils.message("module.success.issuccess"));
    }

}